package library.http;

import java.util.Collections;

import library.http.annotations.BaseUrl;
import library.http.annotations.CertificatePinner;
import library.http.annotations.Debuggable;
import library.http.annotations.Ssl;
import library.http.annotations.SslKey;
import library.http.annotations.SslTrust;
import library.http.annotations.Timeout;
import library.http.ssl.SslConfig;

class HttpTaskProxy extends HttpTask {
    private Class<?> cls;

    public HttpTaskProxy() {

    }

    public HttpTaskProxy(Class<?> cls, HttpAdapter adapter) {
        this.cls = cls;

        BaseUrl baseUrl = cls.getAnnotation(BaseUrl.class);
        Ssl ssl = cls.getAnnotation(Ssl.class);
        SslKey keyStore = cls.getAnnotation(SslKey.class);
        SslTrust trustStore = cls.getAnnotation(SslTrust.class);
        CertificatePinner certificatePinner = cls.getAnnotation(CertificatePinner.class);
        Debuggable debuggable = cls.getAnnotation(Debuggable.class);
        Timeout timeout = cls.getAnnotation(Timeout.class);

        if (baseUrl != null) {
            baseUrl(baseUrl.value());
        }

        SslConfig sslConfig = new SslConfig();
        if (ssl != null)
            sslConfig.ssl(ssl.value());
        if (keyStore != null) {
            sslConfig.sslKey(keyStore.id() != 0 ? keyStore.id() : keyStore.path(), keyStore.pwd(), keyStore.keyType(), keyStore.managerType());
        }
        if (trustStore != null) {
            sslConfig.sslTrust(trustStore.id() != 0 ? trustStore.id() : trustStore.path(), trustStore.pwd(), trustStore.keyType(), trustStore.managerType());
        }

        this.debuggable(debuggable != null ? debuggable.value() : false)
                .client(adapter.getHttpClient())
                .proxy(HttpUtils.getProxy(cls))
                .ssl(sslConfig)
                .converter(Collections.unmodifiableList(adapter.getConverters()));

        if (timeout != null) {
            this.timeout(timeout.connect(), timeout.read(), timeout.write());
        }

        if (certificatePinner != null) {
            this.certificatePinner(certificatePinner.pattern(), certificatePinner.pins());
        }
    }

    protected HttpTaskProxy newInstance() {
        HttpTaskProxy task = new HttpTaskProxy();
        task.debuggable(isDebuggable());
        task.baseUrl(getBaseUrl());
        task.url(getUrl());
        task.tag(getTag());
        task.client(getHttpClient());
        task.ssl(getSslConfig());
        task.cache(getCache());
        task.proxy(getProxy());
        task.certificatePinner(getCertificatePinner());
        task.timeout(getTimeoutConnect(), getTimeoutRead(), getTimeoutWrite());
        task.field(getFields());
        task.query(getQueries());
        task.params(getParams());
        task.headers(getHeaders());
        task.converter(getConverterList());
        task.returnClass(getReturnClass());
        task.returnType(getReturnType());

        if (isMultipart())
            task.multipart(getMultiType());
        else if (isFormUrlEncoded()) {
            task.formUrlEncoded();
        }

        return task;
    }
}